﻿using System;
using System.Data;
using System.Windows.Forms;
using YNCNumberSys.db;
using YNCNumberSys.Entity;
using YNCNumberSys.DbDal;
using cadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace YNCNumberSys
{
    /// <summary>
    /// XSL 检查一遍。
    /// </summary>
    public partial class InitSystemForm : Form
    {
        //CommandManager commandManager = new CommandManager();
        //ConnectionManager connectionManager = new ConnectionManager();

        CodeEntity m_codeentity = null;

        public InitSystemForm()
        {
            InitializeComponent();
            InitFlexGrid();
        }
        private void InitFlexGrid()
        {
            m_flex.Clear();
            m_flex.DataSource = null;
            m_flex.SelectionMode = C1.Win.C1FlexGrid.SelectionModeEnum.ListBox;
            m_flex.Rows.Count = 1;
            m_flex.Cols.Count = 1;

            InitDal initdal = new InitDal();
            DataTable dt=initdal.GetIninitDatable();
            m_flex.DataSource = dt;

            CodeDal codedal = new CodeDal();
            m_codeentity=codedal.GetCodeEntity();
            numericCode.Value = m_codeentity.Digit;
            textBoxLimit.Text = "" + m_codeentity.Limit;

            m_flex.AutoSizeRows();
            m_flex.AutoSizeCols();
        }
        private bool Check()
        {
            bool check = true;
            int digit = (int)numericCode.Value;
            int length = textBoxLimit.Text.Length;
            DataTable dt = (DataTable) m_flex.DataSource;
            //if (digit < m_codeentity.Digit)
            //{
            //    cadApp.ShowAlertDialog("系统已分配图号，只能向上调整！");
            //    check = false;
            //}
            //else
            {
                if (length > digit)
                { //上限的位数（长多）大于位数
                    cadApp.ShowAlertDialog("位数与上限不符！");
                    check = false;
                }
                else
                {
                    if(dt.Rows.Count==1)
                    for(int i=0;i< dt.Columns.Count;i++)
                    {
                        int code=(int)dt.Rows[0][i];
                        if(code >= int.Parse(textBoxLimit.Text))
                        {
                            string info = string.Format("{0}的开始图号必须小于{1}", dt.Columns[i], textBoxLimit.Text);
                            cadApp.ShowAlertDialog(info);
                            check = false;
                        }
                    }
                }
            }
            return check;
        }
        private void btn_确定_Click(object sender, EventArgs e)
        {
            if(Check())
            {
                InitDal initdal = new InitDal();
                DataTable dt = (DataTable)m_flex.DataSource;
                initdal.UpdateIninitDatable(dt);

                CodeEntity entity = new CodeEntity();
                entity.Digit = (int)numericCode.Value;
                entity.Limit = int.Parse(textBoxLimit.Text);
                CodeDal codedal = new CodeDal();
                if(codedal.UpdateCodeEntity(entity))
                {
                    m_codeentity.Digit = entity.Digit;
                    m_codeentity.Limit = entity.Limit;
                }
            }
            else
            {

            }
        }

        private void numericCode_ValueChanged(object sender, EventArgs e)
        {
            int digit = (int)numericCode.Value;
            //if (digit < m_codeentity.Digit)
            //{
            //    cadApp.ShowAlertDialog("系统已分配图号，只能向上调整！");
                //numericCode.Value = m_codeentity.Digit;
            //}
            //else
            //{
                textBoxLimit.Text = (Math.Pow(10.0, (double)digit) - 1).ToString();
            //}
        }
    }
}
